<?php

require_once dirname(__FILE__).'/../lib/trabGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/trabGeneratorHelper.class.php';

/**
 * trab actions.
 *
 * @package    laboratorio
 * @subpackage trab
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class trabActions extends autoTrabActions
{
  public function executeBuscarcodcli(sfWebRequest $request){
    $cid = $request->getParameter('cid');
    if(!empty($cid)){
      $q = Doctrine_Query::create()
        ->select('codigo')
        ->from('Trabajo')
        ->where('cliente_id = '.$cid)
        ->groupBy('id desc')
        ->limit(1);
      $rs = $q->fetchArray();
      if(empty($rs[0])){
        $cod = Doctrine::getTable('Cliente')->find($cid)->getCodigo();
        $num = 1;
      }else{
        list($cod, $num) = explode('-', $rs[0]['codigo']);
        $num += 1;
      }
      return $this->renderText(json_encode($cod.'-'.$num));
    }
  }

  public function executeListDetalle(sfWebRequest $request){
    $this->redirect( 'detrab/index?tid='.$this->getRequestParameter('id'));
  }
  
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

      $resumen = $form->save();
      
      $this->redirect( 'detrab/new?tid='.$resumen->getId());
    }
  }  
}
